<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="教育日期">
        <el-date-picker
          v-model="daterangeEducateTime"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item prop="educateName">
        <el-select v-model="daterangeEducateTime" placeholder="发起人:全部">
          <el-option label="区域一" value="shanghai"></el-option>
      <el-option label="区域二" value="beijing"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item prop="educateName">
        <el-input
          v-model="queryParams.educateName"
          placeholder="搜索教育名称"
          clearable
          suffix-icon="el-icon-search"
          @change="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['educate:educate:add']"
        >新增</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['educate:educate:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['educate:educate:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['educate:educate:export']"
        >导出</el-button>
      </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="educateList" @selection-change="handleSelectionChange">
      <el-table-column label="教育时间" align="center" prop="educateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.educateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="教育名称" align="center" prop="educateName" />
      <el-table-column label="发起人" align="center" prop="createBy" />
      <el-table-column label="作业班组" align="center" prop="educateClass" />
      <el-table-column label="教育影像" align="center" prop="sessionSheet" >
        <template slot-scope="scope">
          <div>
            <i
              class="el-icon-picture"
              style="
                background: rgb(235, 236, 240);
                padding: 5px;
                margin-right: 5px;
              "
            >
              {{ scope.row.photoCount }}
            </i>
            <i
              class="el-icon-video-camera"
              style="
                background: rgb(235, 236, 240);
                padding: 5px;
                margin-right: 5px;
              "
            >
              {{ scope.row.photoCount }}
            </i>
            <i
              class="el-icon-mic"
              style="
                background: rgb(235, 236, 240);
                padding: 5px;
                margin-right: 5px;
              "
            >
              {{ scope.row.photoCount }}
            </i>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="应教育人数" align="center" prop="shouldEducatePeoples" />
      <el-table-column label="实际教育人数" align="center" prop="practiceEducatePeoples" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['educate:educate:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['educate:educate:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改班前教育对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1200px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-tabs>
          <el-tab-pane label="基本信息"></el-tab-pane>
        </el-tabs>
        <el-form-item label="教育名称" prop="educateName">
          <el-input v-model="form.educateName" placeholder="请输入教育名称" />
        </el-form-item>
        <el-form-item label="教育时间" prop="educateTime">
          <el-date-picker 
            clearable
            v-model="form.educateTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择教育时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="作业班组" prop="educateName">
          <el-select multiple @change="handleAddBizEducateClass"  v-model="form.bizEducateClassList" placeholder="请选择">
            <el-option
              v-for="(item,index) in EducateSelectList"
              :key="index"
              :label="item.className"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-tabs>
          <el-tab-pane label="教育资料"></el-tab-pane>
        </el-tabs>
          <!-- <el-input v-model="form.fileName" type="textarea" placeholder="请输入内容" /> -->
          <FileUploadEve  @addNumData="addNumData"></FileUploadEve>
         <el-table ref="FileUploadEveTable" :data="FileUploadEveData">
          <el-table-column label="文件名称" prop="displayName" />
          <!-- <el-table-column label="大小" prop="size" /> -->
          <el-table-column label="上传人" prop="createBy"/> 
          <el-table-column label="上传时间" prop="createTime"/> 
          <el-table-column label="备注" prop="remark">
            <template slot-scope="scope">
              <el-input v-model="scope.row.remark" placeholder="请输入备注" />
            </template>
          </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button
              @click="delFileUploadEveTable(scope.row)"
              type="text"
              >
                删除
              </el-button>
            </template>
          </el-table-column> 
        </el-table>
        <!-- v-model="form.fileBizEducateDocAttachList" -->
        <!-- <el-form-item label="照片名" prop="photoName">
          <el-input v-model="form.photoName" placeholder="请输入照片名" />
        </el-form-item>
        <el-form-item label="会议纪要" prop="sessionSheet">
          <el-input v-model="form.sessionSheet" placeholder="请输入会议纪要" />
        </el-form-item> -->
        <!-- <el-divider content-position="center">教育班级关联信息</el-divider> -->
        <el-tabs>
          <el-tab-pane label="教育记录"></el-tab-pane>
        </el-tabs>
        <!-- <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddBizEducateClass">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteBizEducateClass">删除</el-button>
          </el-col>
        </el-row> -->
         <!-- @selection-change="handleBizEducateClassSelectionChange"  :row-class-name="rowBizEducateClassIndex"  -->
        <el-table :data="bizEducateList" ref="bizEducateClass">
          <el-table-column label="分包单位名称" prop="subPackageCooperatorName" >
          </el-table-column>
          <el-table-column label="班组名称" prop="cooperatorClassName" >
          </el-table-column>
          <el-table-column label="班组长" prop="leaderName" >
          </el-table-column>
          <el-table-column label="实际教育人数" prop="practiceEducatePeoples" >
            <template slot-scope="scope">
              <el-input v-model="scope.row.practiceEducatePeoples" placeholder="请输入实际教育人数" />
            </template>
          </el-table-column>
          <el-table-column label="应教育人数" prop="shouldEducatePeoples" >
            <template slot-scope="scope">
              <el-input v-model="scope.row.shouldEducatePeoples" placeholder="请输入应教育人数" />
            </template>
          </el-table-column>
        </el-table>
        <el-form-item label="会议纪要" prop="sessionSheet">
          <el-input type="textarea" :rows="3" placeholder="请输入会议纪要" v-model="form.sessionSheet"></el-input>
        </el-form-item>
        <el-form-item label="现场照片/视频" prop="educateName">
        <!-- <FileUploadEve  @addNumData="addNumData"></FileUploadEve> -->
          <ImageUploadAdd @addNumDataVio="addNumDataVio"></ImageUploadAdd>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {addEducate, delEducate, getEducate, listEducate, updateEducate,AddlistEducate} from "@/api/biz/educate/educate";
import { mapState } from "vuex";

export default {
  name: "Educate",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedBizEducateClass: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 班前教育表格数据
      educateList: [],
      // 教育班级关联表格数据
      bizEducateList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 应该教育人数时间范围
      daterangeEducateTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        educateName: null,
        educateTime: null,
        createBy: null,
        projectId: this.$store.state.app.selectData,
      },
      // 表单参数
      form: {
        projectId: this.$store.state.app.selectData,
        fileBizEducateDocAttachList:[],//文件附件
        audio:[],//音频附件
        photoName:[],//图片
        fileName:[],
      },
      // 表单校验
      rules: {
      },
      //作业班组下拉
      EducateSelectList:[],
      FileUploadEveData:[],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询班前教育列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeEducateTime && '' != this.daterangeEducateTime) {
        this.queryParams.params["beginEducateTime"] = this.daterangeEducateTime[0];
        this.queryParams.params["endEducateTime"] = this.daterangeEducateTime[1];
      }
      if (this.queryParams.projectId) {
        listEducate(this.queryParams).then(response => {
          this.educateList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      }
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        projectId: this.$store.state.app.selectData,
        id: null,
        educateName: null,
        educateTime: null,
        fileName: [],
        photoName: [],
        sessionSheet: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.bizEducateList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeEducateTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "新建班前教育";
      AddlistEducate(this.queryParams).then(({rows})=>{
        this.EducateSelectList = rows
      })
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getEducate(id).then(response => {
        this.form = response.data;
        this.bizEducateList = response.data.bizEducateClassList;
        this.open = true;
        this.title = "修改班前教育";
      });
    },
    /** 提交按钮 */
    submitForm() {
      console.log(this.form);
      this.form.bizEducateClassList = this.bizEducateList;
      // this.form.audio = this.form.audio.toString()
      this.form.photoName = this.form.photoName.toString()
      this.form.fileName = this.form.fileName.toString()
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.bizEducateClassList = this.bizEducateList;
          if (this.form.id != null) {
            updateEducate(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addEducate(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除班前教育编号为"' + ids + '"的数据项？').then(function() {
        return delEducate(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 教育班级关联序号 */
    rowBizEducateClassIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 教育班级关联添加按钮操作 */
    handleAddBizEducateClass() {
      this.bizEducateList = []
      this.form.bizEducateClassList.forEach(item => {
        let obj = {};
        obj.leaderName = item;
        obj.classId = item;
        obj.subPackageCooperatorName = item;
        obj.cooperatorClassName = item;
        obj.practiceEducatePeoples = "";
        obj.shouldEducatePeoples = "";
        this.bizEducateList.push(obj);
      })
      this.bizEducateList.forEach(item => {
        this.EducateSelectList.forEach(itemName => {
          if (item.classId == itemName.id ) {
            item.leaderName = itemName.jobType
            item.subPackageCooperatorName = itemName.cooperatorName
            item.cooperatorClassName = itemName.className
          }
        })
      })
    },
    /** 教育班级关联删除按钮操作 */
    handleDeleteBizEducateClass() {
      if (this.checkedBizEducateClass.length == 0) {
        this.$modal.msgError("请先选择要删除的教育班级关联数据");
      } else {
        const bizEducateClassList = this.bizEducateList;
        const checkedBizEducateClass = this.checkedBizEducateClass;
        this.bizEducateList = bizEducateClassList.filter(function(item) {
          return checkedBizEducateClass.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleBizEducateClassSelectionChange(selection) {
      this.checkedBizEducateClass = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('educate/educate/export', {
        ...this.queryParams
      }, `educate_${new Date().getTime()}.xlsx`)
    },
    //上传文件删除
    delFileUploadEveTable(){
    },
    addNumData(childNum) {
      this.FileUploadEveData.push(childNum)
      this.form.fileName.push(childNum.fileId)
      },
    addNumDataVio(childNum){
      // childNum.forEach((item)=>{
        // if (item.extName == "mp4") {
        //   this.form.audio.push(item.uid)
        // }
        // else{
          this.form.photoName.push(childNum.fileId)
        // }
      // })
      console.log(childNum);
    },
  },
    watch: {
    app: {
      handler(newVal, oldVal) {
        this.queryParams.projectId = newVal.selectData;
        this.form.projectId = newVal.selectData;
        this.getList();
      },
      deep: true,
    },
    
  },
  computed: {
    ...mapState(["app"]),
  },
};
</script>
